package com.caigou.app

import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.app.databinding.ActivityMainBinding
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.dp2px
import com.caigou.comm.base.ext.openBrowser
import com.caigou.library.floatball.FloatBallCfg
import com.caigou.library.floatball.FloatBallManager
import com.caigou.library.floatball.menu.FloatMenuCfg
import com.caigou.library.floatball.menu.MenuItem
import com.caigou.library.floatball.utils.BackGroudSeletor
import com.caigou.modules.center.ui.CenterFragment
import com.caigou.modules.home.ui.HomeFragment
import com.caigou.modules.info.ui.InfoFragment
import com.caigou.modules.website.ui.WebsiteFragment
import com.dolin.zap.Zap
import com.next.easynavigation.view.EasyNavigationBar
import dagger.hilt.android.AndroidEntryPoint
import kotlin.system.exitProcess

/**
 * @author #Suyghur.
 * Created on 2021/09/14
 */
@AndroidEntryPoint
class MainActivity : BaseActivity() {

    private lateinit var binding: ActivityMainBinding
    private var exitTs = 0L
    private lateinit var floatBallManager: FloatBallManager
    private var currentTab = 0

    private val tabText = arrayOf("菜单", "民间资料", "网站大全", "个人中心")
    private val normalIcon = intArrayOf(R.drawable.tab_home1, R.drawable.tab_info1, R.drawable.tab_website1, R.drawable.tab_center1)
    private val selectIcon = intArrayOf(R.drawable.tab_home2, R.drawable.tab_info2, R.drawable.tab_website2, R.drawable.tab_center2)

    private val fragments: MutableList<Fragment> by lazy {
        mutableListOf(
            HomeFragment(),
            InfoFragment(),
            WebsiteFragment(),
            CenterFragment()
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ARouter.getInstance().inject(this)
        initView()
        initFloatBall()
    }

    private fun initView() {
        binding.easyNavigationBar.titleItems(tabText)
            .mode(EasyNavigationBar.NavigationMode.MODE_ADD)
            .smoothScroll(true)
            .canScroll(true)    //Viewpager能否左右滑动
            .normalIconItems(normalIcon)
            .selectIconItems(selectIcon)
            .fragmentList(fragments)
            .navigationBackground(resources.getColor(R.color.white))   //导航栏背景色
            .selectTextColor(resources.getColor(R.color.caigou_theme_color))   //Tab未选中时字体颜色
            .normalTextColor(resources.getColor(R.color.caigou_gray))   //Tab选中时字体颜色
            .centerImageRes(R.drawable.celebrity)
            .centerTextStr("名人堂")
            .centerIconSize(40)
            .centerSelectTextColor(resources.getColor(R.color.caigou_theme_color))
            .centerNormalTextColor(resources.getColor(R.color.caigou_gray))
            .centerLayoutRule(EasyNavigationBar.RULE_BOTTOM)
            .centerLayoutBottomMargin(0)
            .centerAlignBottom(true)
            .fragmentManager(supportFragmentManager)
            .hasPadding(true)
            .hintPointLeft(-3)  //调节提示红点的位置hintPointLeft hintPointTop（看文档说明）
            .hintPointTop(-3)
            .hintPointSize(10)    //提示红点的大小
            .setOnTabClickListener(object : EasyNavigationBar.OnTabClickListener {
                override fun onTabSelectEvent(view: View?, position: Int): Boolean {
                    currentTab = position
                    if (position == 0) {
                        floatBallManager.show()
                    } else {
                        floatBallManager.hide()
                    }
                    return false
                }

                override fun onTabReSelectEvent(view: View?, position: Int): Boolean {
                    return false
                }
            })
            .setOnCenterTabClickListener {
                ARouter.getInstance().build(RouterPath.Famous.PAGE_WELCOME).navigation()
                false
            }
            .setOnTabLoadListener {
                binding.easyNavigationBar.setHintPoint(3, true)
            }
            .build()
        observeJump2Info()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis().minus(exitTs) <= 2000) {
                finish()
            } else {
                exitTs = System.currentTimeMillis()
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onResume() {
        super.onResume()
        if (currentTab == 0) {
            floatBallManager.show()
        }
    }

    override fun onPause() {
        super.onPause()
        if (currentTab == 0) {
            floatBallManager.hide()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Zap.recycle()
        exitProcess(0)
    }

    private fun observeJump2Info() {
        LiveDataBus.get().with(BusKey.SWITCH_MAIN_PAGE_TAB, Int::class.java).observe(this) {
            binding.easyNavigationBar.viewPager.currentItem = it
        }
    }

    private fun initFloatBall() {
        val ballSize = dp2px(this, 60f)
        val ballIcon = BackGroudSeletor.getdrawble("img_home_bet", this)
        val ballCfg = FloatBallCfg(ballSize, ballIcon, FloatBallCfg.Gravity.RIGHT_CENTER)
        ballCfg.setHideHalfLater(false)
        val menuSize = dp2px(this, 200f)
        val menuItemSize = dp2px(this, 60f)
        val menuCfg = FloatMenuCfg(menuSize, menuItemSize)
        floatBallManager = FloatBallManager(this, ballCfg, menuCfg)
        val ycgjItem = object : MenuItem(R.drawable.img_home_ycgj, "永城国际") {
            override fun action() {
                openBrowser("https://www.ycgj5.com")
                floatBallManager.closeMenu()
            }
        }
        val hcItem: MenuItem = object : MenuItem(R.drawable.img_home_hc, "好彩网") {
            override fun action() {
                openBrowser("https://www.288388nn.com/mobile/#/home")
                floatBallManager.closeMenu()
            }
        }
        floatBallManager.addMenuItem(ycgjItem).addMenuItem(hcItem).buildMenu()
    }
}